﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using OnlineVideos.Data;
using OnlineVideos.Common;
using Common.Library;

using System.ComponentModel;
using Microsoft.Phone.BackgroundAudio;
using System.Windows.Threading;
using Common;
using Common.Utilities;
using System.Windows.Navigation;
//using MyToolkit.Multimedia;
using AdRotator;
using YoutubeApi;

namespace OnlineVideos
{
    public partial class CricketDetail : PhoneApplicationPage
    {
       
        public DispatcherTimer storyboardtimer;
        public CricketDetail()
        {
            try
            {
                InitializeComponent();
                if (!ShowCastManager.ShowGamePivot(AppSettings.ShowID))
                {
                    pvtMainDetails.Items.Remove(gamepivot);
                }
            }
            catch (Exception ex)
            {
                
              Exceptions.SaveOrSendExceptions("Exception in CricketDetail Method In CricketDetail.cs file.", ex);
            }
           
        }
       
        public void addgame()
        {
            try
            {
                Deployment.Current.Dispatcher.BeginInvoke(() =>
                   {
                       if (!ShowCastManager.ShowGameControl(AppSettings.ShowID))
                       {
                           if (GameGrid.Children.Count > 0)
                               GameGrid.Children.RemoveAt(0);
                           MemoryGame showgame = new MemoryGame();
                           GameGrid.Children.Add(showgame);

                           storyboardtimer = new DispatcherTimer();
                           storyboardtimer.Interval = TimeSpan.FromSeconds(2);
                           storyboardtimer.Tick += new EventHandler(storyboardtimer_Tick);
                           storyboardtimer.Start();
                       }

                   });
            }
            catch (Exception ex)
            {
                
                Exceptions.SaveOrSendExceptions("Exception in addgame Method In CricketDetail.cs file.", ex);
            }
        }
        private void pvtMainDetails_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                if (pvtMainDetails.SelectedIndex == 0)
                {
                    Border frameBorder = (Border)VisualTreeHelper.GetChild(Application.Current.RootVisual, 0);
                    AdRotatorControl Adc = frameBorder.FindName("AdRotatorWin8") as AdRotatorControl;
                    Adc.IsAdRotatorEnabled = false;
                    Adc.Visibility = Visibility.Collapsed;
                }
                else
                {
                    Border frameBorder = (Border)VisualTreeHelper.GetChild(Application.Current.RootVisual, 0);
                    AdRotatorControl Adc = frameBorder.FindName("AdRotatorWin8") as AdRotatorControl;
                    Adc.IsAdRotatorEnabled = true;
                    Adc.Visibility = Visibility.Visible;
                    Adc.Invalidate();
                }

                if (pvtMainDetails.SelectedIndex == 2)
                {
                    BackgroundHelper bwhelper = new BackgroundHelper();
                    bwhelper.AddBackgroundTask(
                    (object s, DoWorkEventArgs a) =>
                    {
                        addgame();
                    },
                    (object s, RunWorkerCompletedEventArgs a) =>
                    {

                    }
                    );

                    bwhelper.RunBackgroundWorkers();

                }
                else
                {
                    if (BackgroundAudioPlayer.Instance.PlayerState == PlayState.Playing)
                    {
                        if (BackgroundAudioPlayer.Instance != null && BackgroundAudioPlayer.Instance.Track.Source.ToString().Contains("Claps.mp3"))
                        {
                            BackgroundAudioPlayer.Instance.Stop();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                
                Exceptions.SaveOrSendExceptions("Exception in pvtMainDetails_SelectionChanged Method In CricketDetail.cs file.", ex);
            }
        }
        void storyboardtimer_Tick(object sender, EventArgs e)
        {
            try
            {
                if (pvtMainDetails.SelectedIndex == 2)
                {
                    if (OnlineVideos.Game.GameInstance.gameopened == false)
                    {
                        foreach (OnlineVideos.Game.MemoryCard m in OnlineVideos.Game.GameInstance.stopstoryboard)
                        {
                            m.storyboardfromx = "3";
                            m.storyboardtox = "-3";
                            m.storyboardfromy = "3";
                            m.storyboardtoy = "-3";
                            m.storyboardfromz = "3";
                            m.storyboardtoz = "-3";
                        }
                        storyboardtimer.Stop();
                    }
                }
            }
            catch (Exception ex)
            {
                
                Exceptions.SaveOrSendExceptions("Exception in storyboardtimer_Tick Method In CricketDetail.cs file.", ex);
            }
        }
        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                FlurryWP8SDK.Api.LogEvent("CricketDetail Page", true);
                if (pvtMainDetails.SelectedIndex == 0)
                {
                    Border frameBorder = (Border)VisualTreeHelper.GetChild(Application.Current.RootVisual, 0);
                    AdRotatorControl Adc = frameBorder.FindName("AdRotatorWin8") as AdRotatorControl;
                    Adc.IsAdRotatorEnabled = false;
                    Adc.Visibility = Visibility.Collapsed;
                }

                tblkVideosTitle.Text = CricketMatch.GetMovieTitle(AppSettings.ShowID);
            }
            catch (Exception ex)
            {                
                 Exceptions.SaveOrSendExceptions("Exception in PhoneApplicationPage_Loaded Method In CricketDetail.cs file.", ex);
            }

        }
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {

            try
            {
             
                FlurryWP8SDK.Api.LogPageView();
                if (videos.lbxSongsList.IsEnabled == false)
                    videos.lbxSongsList.IsEnabled = true;
                //YouTube.CancelPlay();
            }
            catch (Exception ex)
            {
                
               Exceptions.SaveOrSendExceptions("Exception in OnNavigatedTo Method In CricketDetail.cs file.", ex);
            }
          
        }
        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            try
            {
                AppState.searchtitle = "";
                FlurryWP8SDK.Api.EndTimedEvent("CricketDetail Page");
            }
            catch (Exception ex)
            {
                
              Exceptions.SaveOrSendExceptions("Exception in OnNavigatedFrom Method In CricketDetail.cs file.", ex);
            }
        }
        protected override void OnBackKeyPress(CancelEventArgs e)
        {
            //if (YouTube.CancelPlay())
            //    e.Cancel = true;

            base.OnBackKeyPress(e);
        }
        private void imgTitle_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
        }
    }
}